<?php
class ControllerModuleArticleAllSettings extends Controller {
	private $error = array(); 
	
	public function index() {   
		$this->load->language('module/article_all_settings');
			$this->load->model('news/install');		
			$this->model_news_install->checkInstall();		
		$this->document->setTitle($this->language->get('heading_title'));
		$this->getList();
	}
	
	public function settings() {   
		$this->load->language('module/article_all_settings');
		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('article_all_settings', $this->request->post);		
					
			$this->session->data['success'] = $this->language->get('text_success');
						
			$this->redirect($this->url->link('module/article_all_settings', 'token=' . $this->session->data['token'], 'SSL'));
		}
			$this->getForm();
	}
	
	private function getList(){			
	
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_setting'] = $this->language->get('text_setting');
		$this->data['text_category'] = $this->language->get('text_category');
		$this->data['text_article'] = $this->language->get('text_article');
		$this->data['text_article_type'] = $this->language->get('text_article_type');
		$this->data['text_author'] = $this->language->get('text_author');
		$this->data['text_download'] = $this->language->get('text_download');
		$this->data['text_comment'] = $this->language->get('text_comment');
		
		$this->data['setting'] = $this->url->link('module/article_all_settings/settings', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['category'] = $this->url->link('news/category', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['article'] = $this->url->link('news/article', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['author'] = $this->url->link('news/author', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['download'] = $this->url->link('news/download', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['comment'] = $this->url->link('news/comment', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['general'] = $this->url->link('module/article_all_settings', 'token=' . $this->session->data['token'], 'SSL');
		
		

		
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		

		
		
		$this->data['text_news_overview'] = $this->language->get('text_news_overview');
		$this->data['text_total_category'] = $this->language->get('text_total_category');
		$this->data['text_total_article'] = $this->language->get('text_total_article');
		$this->data['text_total_author'] = $this->language->get('text_total_author');
		$this->data['text_total_download'] = $this->language->get('text_total_download');
		$this->data['text_total_comment'] = $this->language->get('text_total_comment');
		$this->data['text_total_comment_awaiting'] = $this->language->get('text_total_comment_awaiting');
		
		$this->load->model('news/category');
		$this->load->model('news/article');
		$this->load->model('news/author');
		$this->load->model('news/comment');
		$this->load->model('news/download');
		
		$this->data['total_category'] = $this->model_news_category->getTotalCategories();
		$this->data['total_article'] = $this->model_news_article->getTotalArticle();
		$this->data['total_author'] = $this->model_news_author->getTotalAuthors();
		$this->data['total_download'] = $this->model_news_download->getTotalDownloads();
		$this->data['total_comment'] = $this->model_news_comment->getTotalComments();
		$this->data['total_comment_awaiting'] = $this->model_news_comment->getTotalCommentsAwaitingApproval();
		
		/*Success*/ 
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		/*Error*/ 
		
		
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_module'),
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/article_all_settings', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');

		$this->template = 'module/article_setting_main.tpl';
		$this->children = array(
			'common/header',
			'news/shortcut_route',
			'news/shortcut_module',
			'common/footer'
		);;
				
		$this->response->setOutput($this->render());
	}
	
	private function getForm(){				
	
			$this->load->model('news/install');		
			$this->model_news_install->checkInstall();	
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_enabled'] = $this->language->get('text_enabled');		
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_yes'] = $this->language->get('text_yes');		
		$this->data['text_no'] = $this->language->get('text_no');
		
		$this->data['text_content_top'] = $this->language->get('text_content_top');
		$this->data['text_content_bottom'] = $this->language->get('text_content_bottom');		
		$this->data['text_column_left'] = $this->language->get('text_column_left');
		$this->data['text_column_right'] = $this->language->get('text_column_right');
		
		$this->data['text_description'] = $this->language->get('text_description');
		$this->data['text_featured_article'] = $this->language->get('text_featured_article');
		$this->data['text_image_size'] = $this->language->get('text_image_size');
		$this->data['text_date_format'] = $this->language->get('text_date_format');
		$this->data['text_featured_article'] = $this->language->get('text_featured_article');
		$this->data['text_comment'] = $this->language->get('text_comment');
		$this->data['text_confirm'] = $this->language->get('text_confirm');
          
		
		$this->data['entry_code'] = $this->language->get('entry_code');
		$this->data['entry_layout'] = $this->language->get('entry_layout');
		$this->data['entry_position'] = $this->language->get('entry_position');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_module'] = $this->language->get('button_add_module');
		$this->data['button_remove'] = $this->language->get('button_remove');
		

		
		$this->data['entry_upload_allowed'] = $this->language->get('entry_upload_allowed');
		$this->data['entry_login_to_download'] = $this->language->get('entry_login_to_download');
		$this->data['entry_article_category'] = $this->language->get('entry_article_category');
		$this->data['entry_article_thumb'] = $this->language->get('entry_article_thumb');
		$this->data['entry_article_popup'] = $this->language->get('entry_article_popup');
		$this->data['entry_image_article'] = $this->language->get('entry_image_article');
		$this->data['entry_article_additional'] = $this->language->get('entry_article_additional');
		$this->data['entry_article_related'] = $this->language->get('entry_article_related');
		$this->data['entry_article_description_limit'] = $this->language->get('entry_article_description_limit');
		
		$this->data['entry_free_download'] = $this->language->get('entry_free_download');
		$this->data['entry_comment'] = $this->language->get('entry_comment');
		$this->data['entry_comment_approved'] = $this->language->get('entry_comment_approved');
		$this->data['entry_article_date_format'] = $this->language->get('entry_article_date_format');
		
		/*Success*/ 
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		/*Error*/ 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->error['article_description_limit'])) {
			$this->data['error_article_description_limit'] = $this->error['article_description_limit'];
		} else {
			$this->data['error_article_description_limit'] = '';
		}
		
		if (isset($this->error['image_article'])) {
			$this->data['error_image_article'] = $this->error['image_article'];
		} else {
			$this->data['error_image_article'] = '';
		}
		if (isset($this->error['image_article_category'])) {
			$this->data['error_image_article_category'] = $this->error['image_article_category'];
		} else {
			$this->data['error_image_article_category'] = '';
		}
				
 		if (isset($this->error['image_article_thumb'])) {
			$this->data['error_image_article_thumb'] = $this->error['image_article_thumb'];
		} else {
			$this->data['error_image_article_thumb'] = '';
		}
		
 		if (isset($this->error['image_article_popup'])) {
			$this->data['error_image_article_popup'] = $this->error['image_article_popup'];
		} else {
			$this->data['error_image_article_popup'] = '';
		}
		
 		if (isset($this->error['image_article_additional'])) {
			$this->data['error_image_article_additional'] = $this->error['image_article_additional'];
		} else {
			$this->data['error_image_article_additional'] = '';
		}	
		
 		if (isset($this->error['image_article_related'])) {
			$this->data['error_image_article_related'] = $this->error['image_article_related'];
		} else {
			$this->data['error_image_article_related'] = '';
		}
		
		
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_module'),
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/article_all_settings', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['token'] = $this->session->data['token'];
		$this->data['action'] = $this->url->link('module/article_all_settings/settings', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('module/article_all_settings', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['button_sample'] = $this->language->get('button_sample');
		$this->data['sample']=$this->url->link('module/article_all_settings/install_sample','token='.$this->session->data['token'], 'SSL');
		$this->data['hide_sample']=$this->url->link('module/article_all_settings/hide_sample','token='.$this->session->data['token'], 'SSL');
	/*General Settings*/ 
	
		if (isset($this->request->post['config_news_sample_installed'])) {
			$this->data['config_news_sample_installed'] = $this->request->post['config_news_sample_installed'];
		} else {
			$this->data['config_news_sample_installed'] = $this->config->get('config_news_sample_installed');
		}
		if (isset($this->request->post['config_news_installed'])) {
			$this->data['config_news_installed'] = $this->request->post['config_news_installed'];
		} else {
			$this->data['config_news_installed'] = $this->config->get('config_news_installed');
		}
		/*Date fotmat*/ 
		if (isset($this->request->post['config_article_date_format'])) {
			$this->data['config_article_date_format'] = $this->request->post['config_article_date_format'];
		} else {
			$this->data['config_article_date_format'] = $this->config->get('config_article_date_format');
		}
		/*featured article*/ 
		$this->load->model('news/category');
				
		$this->data['categories'] = $this->model_news_category->getCategories(0);
		if (isset($this->request->post['featured_article'])) {
      		$this->data['featured_article'] = $this->request->post['featured_article'];
    	} else {
      		$this->data['featured_article'] = $this->config->get('featured_article');
		}
		/*Downloads*/ 
		if (isset($this->request->post['config_news_upload_allowed'])) {
			$this->data['config_news_upload_allowed'] = $this->request->post['config_news_upload_allowed'];
		} else {
			$this->data['config_news_upload_allowed'] = $this->config->get('config_news_upload_allowed');
		}
		if (isset($this->request->post['config_login_to_download'])) {
			$this->data['config_login_to_download'] = $this->request->post['config_login_to_download'];
		} else {
			$this->data['config_login_to_download'] = $this->config->get('config_login_to_download');
		}
		/*free download*/
		if (isset($this->request->post['config_free_download'])) {
			$this->data['config_free_download'] = $this->request->post['config_free_download'];
		} else {
			$this->data['config_free_download'] = $this->config->get('config_free_download');
		}	
				
		$this->load->model('news/download');
				
		if (isset($this->request->post['free_download'])) {
			$downloads = explode(',', $this->request->post['config_free_download']);
		} else {		
			$downloads = explode(',', $this->config->get('config_free_download'));
		}
		
		$this->data['downloads'] = array();
		
		foreach ($downloads as $download_id) {
			$download_info = $this->model_news_download->getFreeDownload($download_id);
			
			if ($download_info) {
				$this->data['downloads'][] = array(
					'download_id' => $download_info['download_id'],
					'name'       => $download_info['name']
				);
			}
		}	
		/*Comment allow*/ 
		if (isset($this->request->post['config_comment_status'])) {
			$this->data['config_comment_status'] = $this->request->post['config_comment_status'];
		} else {
			$this->data['config_comment_status'] = $this->config->get('config_comment_status');
		}
		if (isset($this->request->post['config_comment_approved'])) {
			$this->data['config_comment_approved'] = $this->request->post['config_comment_approved'];
		} else {
			$this->data['config_comment_approved'] = $this->config->get('config_comment_approved');
		}
		/*Article Image*/ 
		if (isset($this->request->post['article_description_limit'])) {
			$this->data['article_description_limit'] = $this->request->post['article_description_limit'];
		}elseif($this->config->get('article_description_limit')) {
			$this->data['article_description_limit'] = $this->config->get('article_description_limit');
		}else {
			$this->data['article_description_limit'] = 255;
		}
		
		if (isset($this->request->post['config_article_category_width'])) {
			$this->data['config_article_category_width'] = $this->request->post['config_article_category_width'];
		} else {
			$this->data['config_article_category_width'] = $this->config->get('config_article_category_width');
		}
		
		if (isset($this->request->post['config_article_category_height'])) {
			$this->data['config_article_category_height'] = $this->request->post['config_article_category_height'];
		} else {
			$this->data['config_article_category_height'] = $this->config->get('config_article_category_height');
		}
				
		if (isset($this->request->post['config_article_thumb_width'])) {
			$this->data['config_article_thumb_width'] = $this->request->post['config_article_thumb_width'];
		} else {
			$this->data['config_article_thumb_width'] = $this->config->get('config_article_thumb_width');
		}
		
		if (isset($this->request->post['config_article_thumb_height'])) {
			$this->data['config_article_thumb_height'] = $this->request->post['config_article_thumb_height'];
		} else {
			$this->data['config_article_thumb_height'] = $this->config->get('config_article_thumb_height');
		}
		
		if (isset($this->request->post['config_article_popup_width'])) {
			$this->data['config_article_popup_width'] = $this->request->post['config_article_popup_width'];
		} else {
			$this->data['config_article_popup_width'] = $this->config->get('config_article_popup_width');
		}
		
		if (isset($this->request->post['config_article_popup_height'])) {
			$this->data['config_article_popup_height'] = $this->request->post['config_article_popup_height'];
		} else {
			$this->data['config_article_popup_height'] = $this->config->get('config_article_popup_height');
		}
		
		if (isset($this->request->post['config_article_additional_width'])) {
			$this->data['config_article_additional_width'] = $this->request->post['config_article_additional_width'];
		} else {
			$this->data['config_article_additional_width'] = $this->config->get('config_article_additional_width');
		}
		
		if (isset($this->request->post['config_article_additional_height'])) {
			$this->data['config_article_additional_height'] = $this->request->post['config_article_additional_height'];
		} else {
			$this->data['config_article_additional_height'] = $this->config->get('config_article_additional_height');
		}
		
		if (isset($this->request->post['config_article_related_width'])) {
			$this->data['config_article_related_width'] = $this->request->post['config_article_related_width'];
		} else {
			$this->data['config_article_related_width'] = $this->config->get('config_article_related_width');
		}
		
		if (isset($this->request->post['config_article_related_height'])) {
			$this->data['config_article_related_height'] = $this->request->post['config_article_related_height'];
		} else {
			$this->data['config_article_related_height'] = $this->config->get('config_article_related_height');
		}
		


		if (isset($this->request->post['config_image_article_width'])) {
			$this->data['config_image_article_width'] = $this->request->post['config_image_article_width'];
		} else {
			$this->data['config_image_article_width'] = $this->config->get('config_image_article_width');
		}
		
		if (isset($this->request->post['config_image_article_height'])) {
			$this->data['config_image_article_height'] = $this->request->post['config_image_article_height'];
		} else {
			$this->data['config_image_article_height'] = $this->config->get('config_image_article_height');
		}
		/*general settings*/ 	
		
		$this->data['modules'] = array();
		
		if (isset($this->request->post['article_all_settings_module'])) {
			$this->data['modules'] = $this->request->post['article_all_settings_module'];
		} elseif ($this->config->get('article_all_settings_module')) { 
			$this->data['modules'] = $this->config->get('article_all_settings_module');
		}			
				
		$this->load->model('design/layout');
		
		$this->data['layouts'] = $this->model_design_layout->getLayouts();

		$this->template = 'module/article_setting_form.tpl';
		$this->children = array(
			'common/header',
			'news/shortcut_route',
			'news/shortcut_module',
			'common/footer'
		);;
				
		$this->response->setOutput($this->render());
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/article_all_settings')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		/*General Setting*/ 	
		if (($this->request->post['article_description_limit']) < 3 || ($this->request->post['article_description_limit']) > 1024) {
			$this->error['article_description_limit'] = $this->language->get('error_article_description_limit');
		}
							
		if (!$this->request->post['config_article_category_width'] || !$this->request->post['config_article_category_height']) {
			$this->error['image_article_category'] = $this->language->get('error_image_article_category');
		} 
				
		if (!$this->request->post['config_article_thumb_width'] || !$this->request->post['config_article_thumb_height']) {
			$this->error['image_article_thumb'] = $this->language->get('error_image_article_thumb');
		}	
		
		if (!$this->request->post['config_article_popup_width'] || !$this->request->post['config_article_popup_height']) {
			$this->error['image_article_popup'] = $this->language->get('error_image_article_popup');
		}	
		
		if (!$this->request->post['config_image_article_width'] || !$this->request->post['config_image_article_height']) {
			$this->error['image_article'] = $this->language->get('error_image_article');
		}		
		
		if (!$this->request->post['config_article_additional_width'] || !$this->request->post['config_article_additional_height']) {
			$this->error['image_article_additional'] = $this->language->get('error_image_article_additional');
		}
		
		if (!$this->request->post['config_article_related_width'] || !$this->request->post['config_article_related_height']) {
			$this->error['image_article_related'] = $this->language->get('error_image_article_related');
		}
		/*general*/ 
		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}	
	
	public function category() {
		$this->load->model('news/article');
		
		if (isset($this->request->get['article_category_id'])) {
			$article_category_id = $this->request->get['article_category_id'];
		} else {
			$article_category_id = 0;
		}
		
		$article_data = array();
		
		$results = $this->model_news_article->getArticlesByCategoryId($article_category_id);
		
		foreach ($results as $result) {
			$article_data[] = array(
				'article_id' => $result['article_id'],
				'name'       => $result['name']
			);
		}
		
		$this->response->setOutput(json_encode($article_data));
	}
	
	public function featured() {
		$this->load->model('news/article');
		
		if (isset($this->request->post['featured_article'])) {
			$articles = $this->request->post['featured_article'];
		} else {
			$articles = array();
		}
	
		$article_data = array();
		
		foreach ($articles as $article_id) {
			$article_info = $this->model_news_article->getArticle($article_id);
			
			if ($article_info) {
				$article_data[] = array(
					'article_id' => $article_info['article_id'],
					'name'       => $article_info['name']
				);
			}
		}
		
		$this->response->setOutput(json_encode($article_data));
	}
	public function install_sample(){
		$this->load->model('news/install');
		$this->model_news_install->installSampleNews();
		$this->session->data['success']='Success: Successfully installed sample database!';
		$this->redirect($this->url->link('module/article_all_settings/settings','token='.$this->session->data['token'], 'SSL'));
	}
	
	public function hide_sample(){
		$this->load->model('news/install');
		$this->model_news_install->hideSampleNews();
		$this->redirect($this->url->link('module/article_all_settings/settings','token='.$this->session->data['token'], 'SSL'));
	}
}
?>